<script setup lang="ts">

import router from "@/router";
import {ref} from "vue";
import {getCaptcha, getInfo, reviseInfo} from "@/api/home/login/login";
import {ElMessage} from "element-plus";
import {ArrowRight} from '@element-plus/icons-vue'

const textarea = ref('')
const textareaInput = ref('')
let user = JSON.parse(localStorage.getItem('userInformation'))
textarea.value = user.phoneNumber

let displayXg = ref(true)
let captcha = ref('')

let information = JSON.parse(localStorage.getItem('userInformation'))

function areYouSure() {
    displayXg.value = !displayXg.value
}

function replacement() {
    if (displayXg.value) {
        information.nickname = textarea.value
        reviseInfo(information).then(res => {
            if (res.code == 200) {
                ElMessage({
                    message: '修改成功',
                    type: 'success',
                })
                router.go(-1)
            }
        })
        getInfo().then(info => {
            console.log(info)
            if (info.code == 200) {
                console.log('123')
                localStorage.setItem('userInformation', JSON.stringify(information))
            }
        })
    }
}


import {computed, onMounted} from 'vue';

const captchan = ref('');
const countdown = ref(0);
const isCountingDown = ref(false);
const timerId = ref();
// 生成随机验证码
const generateCaptcha = () => {
    captchan.value = Math.random().toString(36).substring(7);
};

// 开始倒计时
const startCountdown = () => {
    isCountingDown.value = true;
    countdown.value = 60;
    timerId.value = setInterval(() => {
        if (countdown.value > 0) {
            countdown.value--;
        } else {
            clearInterval(timerId.value);
            timerId.value = null;
            isCountingDown.value = false;
            generateCaptcha();
        }
    }, 1000);
};

// 格式化倒计时显示
const formattedCountdown = computed(() => {
    if (isCountingDown.value && countdown.value > 0) {
        return `${countdown.value}s`;
    } else {
        return '';
    }
});

// 点击按钮时生成验证码并开始倒计时
const generateCodeAndStartCountdown = () => {
    if (!isCountingDown.value) {
        generateCaptcha();
        startCountdown();
        getCaptcha(textarea.value).then(res=>{
            console.log(res)
        })
    }
};

// 在组件挂载和卸载时清除可能的定时器
onMounted(() => {
    return () => {
        if (timerId.value) {
            clearInterval(timerId.value);
            timerId.value = null;
        }
    };
});

</script>

<template>
  <!--修改手机号-->
    <div class="box">
        <div class="titTime" @click="router.go(-1)">
            <van-icon name="arrow-left"/>
            <span class="titleText">手机号设置</span>
        </div>
        <div class="substance">
            <div class="mobilePhoneNumber" @click="router.push({path: '/reviseCellPhone'})" v-if="displayXg">
                <div class="Phone">
                    当前手机号
                </div>
                <div class="number">
                    {{ textarea }}
                    <el-icon>
                        <ArrowRight/>
                    </el-icon>
                </div>
            </div>
            <div class="revise" v-else>
                <div class="phoneBox">
                    <div class="Phone">
                        绑定手机号
                    </div>
                    <div class="number">
                        <input type="text" v-model="textareaInput" class="inputNumber" placeholder="请输入您的手机号">
                        <div class="captcha" :disabled="isCountingDown" style="color: #419CFD" @click="generateCodeAndStartCountdown">
                            <span v-if="!isCountingDown">获取验证码</span>
                            <span v-else>{{ formattedCountdown }}</span>
                        </div>
                    </div>
                </div>
                <div class="captchaBox">
                    <div class="phoneBox">
                        <div class="Phone">
                            验证码
                        </div>
                        <div class="number">
                            <input type="text" v-model="captcha" class="inputNumber" placeholder="请输入验证码">
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="uploadBtn" v-if="displayXg">
            <el-button style="width: 50vw" type="primary" @click="areYouSure">更换手机号</el-button>
        </div>
        <div class="reviseBtn" v-else>
            <el-button style="width: 50vw" type="primary" @click="replacement">提交</el-button>
        </div>
    </div>
</template>

<style scoped>
* {
    margin: 0;
    padding: 0;
}

.box {
    padding: 8% 6%;

    .titTime {
        margin-bottom: 5%;
        font-size: 5vw;
    }
}

.substance {
    margin-top: 3%;
    padding: 0 5%;
    width: 100%;
    height: 100%;
    min-height: 48vh;

    .mobilePhoneNumber {
        padding: 6% 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid rgba(42, 43, 65, 0.1);

        .number {
            display: flex;
            align-items: center;
        }
    }

    .revise {
        .phoneBox {
            padding: 5% 0;
            border-bottom: 1px solid rgba(42, 43, 65, 0.1);
        }

        .Phone {
            margin-bottom: 3%;
            font-size: 5vw;
        }

        .number {
            display: flex;

            .inputNumber {
                border: none;
            }
        }

    }
}

.uploadBtn {
    margin: 8% auto;
    padding: 0 20%;
}

.reviseBtn {
    margin: 8% auto;
    padding: 0 20%;
}
</style>